home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / lib / glut / glut_menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  26.9 KB  |  993 lines

  1. /* Copyright (c) Mark J. Kilgard, 1994. */
  2.  
  3. /* This program is freely distributable without licensing fees
  4.    and is provided without guarantee or warrantee expressed or
  5.    implied. This program is -not- in the public domain. */
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include <assert.h>
  13.  
  14. #include <X11/Xlib.h>
  15. #include <X11/cursorfont.h>  /* for XC_arrow */
  16.  
  17. #include <GL/glut.h>
  18. #include "glutint.h"
  19. #include "layerutil.h"
  20.  
  21. GLUTmenu *__glutCurrentMenu = NULL;
  22. void (*__glutMenuStatusFunc) (int, int, int);
  23. GLUTmenu *__glutMappedMenu;
  24. GLUTwindow *__glutMenuWindow;
  25. GLUTmenuItem *__glutItemSelected;
  26.  
  27. static GLUTmenu **menuList = NULL;
  28. static int menuListSize = 0;
  29. static XFontStruct *menuFont = NULL;
  30. static Cursor menuCursor;
  31. static Colormap menuColormap;
  32. static Visual *menuVisual;
  33. static int menuDepth;
  34. static int fontHeight;
  35. static GC blackGC, grayGC, whiteGC;
  36. static unsigned long menuBlack, menuWhite, menuGray;
  37. static unsigned long useSaveUnders;
  38.  
  39. /* A replacement for XAllocColor (originally by Brian Paul).
  40.    This  function should never fail to allocate a color.  When
  41.    XAllocColor fails, we return the nearest matching color.  If
  42.    we have to allocate many colors this function isn't a great
  43.    solution; the XQueryColors() could be done just once.  */
  44. static void
  45. noFaultXAllocColor(Display * dpy, Colormap cmap, int cmapSize,
  46.   XColor * color)
  47. {
  48.   XColor *ctable, subColor;
  49.   int i, bestmatch;
  50.   double mindist;       /* 3*2^16^2 exceeds long int precision. 
  51.                          */
  52.  
  53.   for (;;) {
  54.     /* First try just using XAllocColor. */
  55.     if (XAllocColor(dpy, cmap, color))
  56.       return;
  57.  
  58.     /* Retrieve color table entries. */
  59.     /* XXX alloca canidate. */
  60.     ctable = (XColor *) malloc(cmapSize * sizeof(XColor));
  61.     for (i = 0; i < cmapSize; i++)
  62.       ctable[i].pixel = i;
  63.     XQueryColors(dpy, cmap, ctable, cmapSize);
  64.  
  65.     /* Find best match. */
  66.     bestmatch = -1;
  67.     mindist = 0.0;
  68.     for (i = 0; i < cmapSize; i++) {
  69.       double dr = (double) color->red - (double) ctable[i].red;
  70.       double dg = (double) color->green - (double) ctable[i].green;
  71.       double db = (double) color->blue - (double) ctable[i].blue;
  72.       double dist = dr * dr + dg * dg + db * db;
  73.       if (bestmatch < 0 || dist < mindist) {
  74.         bestmatch = i;
  75.         mindist = dist;
  76.       }
  77.     }
  78.  
  79.     /* Return result. */
  80.     subColor.red = ctable[bestmatch].red;
  81.     subColor.green = ctable[bestmatch].green;
  82.     subColor.blue = ctable[bestmatch].blue;
  83.     free(ctable);
  84.     if (XAllocColor(dpy, cmap, &subColor)) {
  85.       *color = subColor;
  86.       return;
  87.     }
  88.     /* Extremely unlikely, but possibly color was deallocated
  89.        and reallocated by someone else before we could
  90.        XAllocColor the color cell we located.  If so, loop
  91.        again... */
  92.   }
  93. }
  94.  
  95. static void
  96. menuVisualSetup(void)
  97. {
  98.   XLayerVisualInfo template, *visual, *overlayVisuals;
  99.   XColor color;
  100.   Status status;
  101.   Bool presumablyMesa;
  102.   int layer, nVisuals, i, dummy;
  103.  
  104.   for (layer = 3; layer > 0; layer--) {
  105.     template.layer = layer;
  106.     template.vinfo.screen = __glutScreen;
  107.     overlayVisuals = __glutXGetLayerVisualInfo(__glutDisplay,
  108.       VisualScreenMask | VisualLayerMask, &template, &nVisuals);
  109.     if (overlayVisuals) {
  110.       for (i = 0; i < nVisuals; i++) {
  111.         visual = &overlayVisuals[i];
  112.         if (visual->vinfo.colormap_size >= 3) {
  113.           menuColormap = XCreateColormap(__glutDisplay, __glutRoot,
  114.             visual->vinfo.visual, AllocNone);
  115.           /* Allocate overlay colormap cells in defined order:
  116.              gray, black, white to match the IRIS GL allocation
  117.              scheme.  Increases likelihood of less overlay
  118.              colormap flashing. */
  119.           /* XXX nice if these 3 AllocColor's could be done in
  120.              one protocol round-trip */
  121.           color.red = color.green = color.blue = 0xaa00;
  122.           status = XAllocColor(__glutDisplay,
  123.             menuColormap, &color);
  124.           if (!status) {
  125.             XFreeColormap(__glutDisplay, menuColormap);
  126.             continue;
  127.           }
  128.           menuGray = color.pixel;
  129.           color.red = color.green = color.blue = 0x0000;
  130.           status = XAllocColor(__glutDisplay,
  131.             menuColormap, &color);
  132.           if (!status) {
  133.             XFreeColormap(__glutDisplay, menuColormap);
  134.             continue;
  135.           }
  136.           menuBlack = color.pixel;
  137.           color.red = color.green = color.blue = 0xffff;
  138.           status = XAllocColor(__glutDisplay,
  139.             menuColormap, &color);
  140.           if (!status) {
  141.             XFreeColormap(__glutDisplay, menuColormap);
  142.             continue;
  143.           }
  144.           menuWhite = color.pixel;
  145.           menuVisual = visual->vinfo.visual;
  146.           menuDepth = visual->vinfo.depth;
  147.           /* If using overlays, avoid requesting "save unders". 
  148.            */
  149.           useSaveUnders = 0;
  150.           XFree(overlayVisuals);
  151.           return;
  152.         }
  153.       }
  154.       XFree(overlayVisuals);
  155.     }
  156.   }
  157.   /* settle for default visual */
  158.   menuVisual = DefaultVisual(__glutDisplay, __glutScreen);
  159.   menuDepth = DefaultDepth(__glutDisplay, __glutScreen);
  160.   menuColormap = DefaultColormap(__glutDisplay, __glutScreen);
  161.   menuBlack = BlackPixel(__glutDisplay, __glutScreen);
  162.   menuWhite = WhitePixel(__glutDisplay, __glutScreen);
  163.   color.red = color.green = color.blue = 0xaa00;
  164.   noFaultXAllocColor(__glutDisplay, menuColormap,
  165.     menuVisual->map_entries, &color);
  166.   menuGray = color.pixel;
  167.  
  168.   /* When no overlays are supported, we would like to use X
  169.      "save unders" to avoid exposes to windows obscured by
  170.      pop-up menus.  However, OpenGL's direct rendering support
  171.      means OpenGL interacts poorly with X backing store and
  172.      save unders.  X servers do not (in implementation
  173.      practice) redirect OpenGL rendering destined to obscured
  174.      window regions into backing store.
  175.  
  176.      Implementation solutions exist for this problem, but they
  177.      are expensive and high-end OpenGL implementations
  178.      typically provide fast rendering and/or overlays to
  179.      obviate the problem associated of user interfaces (pop-up
  180.      menus) forcing redraws of complex normal plane scenes.
  181.      (See support for overlays pop-up menus above.)
  182.  
  183.      Mesa 3D, however, does not support direct rendering.
  184.      Overlays are often unavailable to Mesa, and Mesa is also
  185.      relatively slow.  For these reasons, Mesa-rendering GLUT
  186.      programs can and should use X save unders.
  187.  
  188.      Look for the GLX extension.  If _not_ supported, we are
  189.      presumably using Mesa so enable save unders. */
  190.  
  191.   presumablyMesa = !XQueryExtension(__glutDisplay, "GLX",
  192.     &dummy, &dummy, &dummy);
  193.  
  194.   if (presumablyMesa)
  195.     useSaveUnders = CWSaveUnder;
  196.   else
  197.     useSaveUnders = 0;
  198. }
  199.  
  200. static void
  201. menuSetup(void)
  202. {
  203.   if (menuFont) {
  204.     /* menuFont overload to indicate menu initalization */
  205.     return;
  206.   }
  207.   menuFont = XLoadQueryFont(__glutDisplay,
  208.     "-*-helvetica-bold-o-normal--14-*-*-*-p-*-iso8859-1");
  209.   if (!menuFont) {
  210.     /* try back up font */
  211.     menuFont = XLoadQueryFont(__glutDisplay, "fixed");
  212.   }
  213.   if (!menuFont) {
  214.     __glutFatalError("could not load font.");
  215.   }
  216.   menuVisualSetup();
  217.   fontHeight = menuFont->ascent + menuFont->descent;
  218.   menuCursor = XCreateFontCursor(__glutDisplay, XC_arrow);
  219. }
  220.  
  221. static void
  222. menuGraphicsContextSetup(Window win)
  223. {
  224.   XGCValues gcvals;
  225.  
  226.   if (blackGC != None)
  227.     return;
  228.   gcvals.font = menuFont->fid;
  229.   gcvals.foreground = menuBlack;
  230.   blackGC = XCreateGC(__glutDisplay, win,
  231.     GCFont | GCForeground, &gcvals);
  232.   gcvals.foreground = menuGray;
  233.   grayGC = XCreateGC(__glutDisplay, win, GCForeground, &gcvals);
  234.   gcvals.foreground = menuWhite;
  235.   whiteGC = XCreateGC(__glutDisplay, win, GCForeground, &gcvals);
  236. }
  237.  
  238. /* OBSOLETE - use glutMenuStatusFunc instead. */
  239. void
  240. glutMenuStateFunc(GLUTmenuStateCB menuStateFunc)
  241. {
  242.   __glutMenuStatusFunc = (GLUTmenuStatusCB) menuStateFunc;
  243. }
  244.  
  245. void
  246. glutMenuStatusFunc(GLUTmenuStatusCB menuStatusFunc)
  247. {
  248.   __glutMenuStatusFunc = menuStatusFunc;
  249. }
  250.  
  251. void
  252. __glutSetMenu(GLUTmenu * menu)
  253. {
  254.   __glutCurrentMenu = menu;
  255. }
  256.  
  257. static void
  258. unmapMenu(GLUTmenu * menu)
  259. {
  260.   if (menu->cascade) {
  261.     unmapMenu(menu->cascade);
  262.     menu->cascade = NULL;
  263.   }
  264.   menu->anchor = NULL;
  265.   menu->highlighted = NULL;
  266.   XUnmapWindow(__glutDisplay, menu->win);
  267. }
  268.  
  269. void
  270. __glutFinishMenu(Window win, int x, int y)
  271. {
  272.   Window dummy;
  273.   int rc;
  274.  
  275.   unmapMenu(__glutMappedMenu);
  276.   XUngrabPointer(__glutDisplay, CurrentTime);
  277.  
  278.   /* This XFlush is needed to to make sure the pointer is
  279.      really ungrabbed when the application's menu callback is
  280.      called. Otherwise, a deadlock might happen because the
  281.      application may try to read from an terminal window, but
  282.      yet the ungrab hasn't really happened since it hasn't been
  283.      flushed out. */
  284.   XFlush(__glutDisplay);
  285.  
  286.   if (__glutMenuStatusFunc) {
  287.     if (win != __glutMenuWindow->win) {
  288.       /* The button release may have occurred in a window other
  289.          than the window requesting the pop-up menu (for
  290.          example, one of the submenu windows).  In this case, we
  291.          need to translate the coordinates into the coordinate
  292.          system of the window associated with the window. */
  293.       rc = XTranslateCoordinates(__glutDisplay, win, __glutMenuWindow->win,
  294.         x, y, &x, &y, &dummy);
  295.       assert(rc != False);  /* Will always be on same screen. */
  296.     }
  297.     __glutSetWindow(__glutMenuWindow);
  298.     __glutSetMenu(__glutMappedMenu);
  299.  
  300.     /* Setting __glutMappedMenu to NULL permits operations that
  301.        change menus or destroy the menu window again. */
  302.     __glutMappedMenu = NULL;
  303.  
  304.     __glutMenuStatusFunc(GLUT_MENU_NOT_IN_USE, x, y);
  305.   }
  306.   /* Setting __glutMappedMenu to NULL permits operations that
  307.      change menus or destroy the menu window again. */
  308.   __glutMappedMenu = NULL;
  309.  
  310.   /* If an item is selected and it is not a submenu trigger,
  311.      generate menu callback. */
  312.   if (__glutItemSelected && !__glutItemSelected->isTrigger) {
  313.     __glutSetWindow(__glutMenuWindow);
  314.     /* When menu callback is triggered, current menu should be
  315.        set to the callback menu. */
  316.     __glutSetMenu(__glutItemSelected->menu);
  317.     __glutItemSelected->menu->select(
  318.       __glutItemSelected->value);
  319.   }
  320.   __glutMenuWindow = NULL;
  321. }
  322.  
  323. #define MENU_BORDER 1
  324. #define MENU_GAP 2
  325. #define MENU_ARROW_GAP 6
  326. #define MENU_ARROW_WIDTH 8
  327.  
  328. static void
  329. mapMenu(GLUTmenu * menu, int x, int y)
  330. {
  331.   XWindowChanges changes;
  332.   unsigned int mask;
  333.   int subMenuExtension, num;
  334.  
  335.   /* If there are submenus, we need to provide extra space for
  336.      the submenu pull arrow.  */
  337.   if (menu->submenus > 0) {
  338.     subMenuExtension = MENU_ARROW_GAP + MENU_ARROW_WIDTH;
  339.   } else {
  340.     subMenuExtension = 0;
  341.   }
  342.  
  343.   changes.stack_mode = Above;
  344.   mask = CWStackMode | CWX | CWY;
  345.   /* If the menu isn't managed (ie, validated so all the
  346.      InputOnly subwindows are the right size), do so.  */
  347.   if (!menu->managed) {
  348.     GLUTmenuItem *item;
  349.  
  350.     item = menu->list;
  351.     num = menu->num;
  352.     while (item) {
  353.       XWindowChanges itemupdate;
  354.  
  355.       itemupdate.y = (num - 1) * fontHeight + MENU_GAP;
  356.       itemupdate.width = menu->pixwidth;
  357.       itemupdate.width += subMenuExtension;
  358.       XConfigureWindow(__glutDisplay, item->win,
  359.         CWWidth | CWY, &itemupdate);
  360.       item = item->next;
  361.       num--;
  362.     }
  363.     menu->pixheight = MENU_GAP +
  364.       fontHeight * menu->num + MENU_GAP;
  365.     changes.height = menu->pixheight;
  366.     changes.width = MENU_GAP +
  367.       menu->pixwidth + subMenuExtension + MENU_GAP;
  368.     mask |= CWWidth | CWHeight;
  369.     menu->managed = True;
  370.   }
  371.   /* make sure menu appears fully on screen */
  372.   if (y + menu->pixheight >= __glutScreenHeight) {
  373.     changes.y = __glutScreenHeight - menu->pixheight;
  374.   } else {
  375.     changes.y = y;
  376.   }
  377.   if (x + menu->pixwidth + subMenuExtension >=
  378.     __glutScreenWidth) {
  379.     changes.x = __glutScreenWidth -
  380.       menu->pixwidth + subMenuExtension;
  381.   } else {
  382.     changes.x = x;
  383.   }
  384.  
  385.   /* Rember where the menu is placed so submenus can be
  386.      properly placed relative to it. */
  387.   menu->x = changes.x;
  388.   menu->y = changes.y;
  389.  
  390.   XConfigureWindow(__glutDisplay, menu->win, mask, &changes);
  391.   XInstallColormap(__glutDisplay, menuColormap);
  392.   /* XXX The XRaiseWindow below should not be necessary because
  393.      the XConfigureWindow requests an Above stack mode (same as
  394.      XRaiseWindow), but some Sun users complained this was still
  395.      necessary.  Probably some window manager or X server bug on
  396.      these machines?? */
  397.   XRaiseWindow(__glutDisplay, menu->win);
  398.   XMapWindow(__glutDisplay, menu->win);
  399. }
  400.  
  401. void
  402. __glutStartMenu(GLUTmenu * menu, GLUTwindow * window,
  403.   int x, int y, int x_win, int y_win)
  404. {
  405.   int grab;
  406.  
  407.   assert(__glutMappedMenu == NULL);
  408.   grab = XGrabPointer(__glutDisplay, __glutRoot, True,
  409.     ButtonPressMask | ButtonReleaseMask,
  410.     GrabModeAsync, GrabModeAsync,
  411.     __glutRoot, menuCursor, CurrentTime);
  412.   if (grab != GrabSuccess) {
  413.     /* Somebody else has pointer grabbed, ignore menu
  414.        activation. */
  415.     return;
  416.   }
  417.   __glutMappedMenu = menu;
  418.   __glutMenuWindow = window;
  419.   __glutItemSelected = NULL;
  420.   if (__glutMenuStatusFunc) {
  421.     __glutSetMenu(menu);
  422.     __glutSetWindow(window);
  423.     __glutMenuStatusFunc(GLUT_MENU_IN_USE, x_win, y_win);
  424.   }
  425.   mapMenu(menu, x, y);
  426. }
  427.  
  428. static void
  429. paintSubMenuArrow(Window win, int x, int y)
  430. {
  431.   XPoint p[5];
  432.  
  433.   p[0].x = p[4].x = x;
  434.   p[0].y = p[4].y = y - menuFont->ascent + 1;
  435.   p[1].x = p[0].x + MENU_ARROW_WIDTH - 1;
  436.   p[1].y = p[0].y + (menuFont->ascent / 2) - 1;
  437.   p[2].x = p[1].x;
  438.   p[2].y = p[1].y + 1;
  439.   p[3].x = p[0].x;
  440.   p[3].y = p[0].y + menuFont->ascent - 2;
  441.   XFillPolygon(__glutDisplay, win,
  442.     whiteGC, p, 4, Convex, CoordModeOrigin);
  443.   XDrawLines(__glutDisplay, win, blackGC, p, 5, CoordModeOrigin);
  444. }
  445.  
  446. static void
  447. paintMenuItem(GLUTmenuItem * item, int num)
  448. {
  449.   Window win = item->menu->win;
  450.   GC gc;
  451.   int y;
  452.   int subMenuExtension;
  453.  
  454.   if (item->menu->submenus > 0) {
  455.     subMenuExtension = MENU_ARROW_GAP + MENU_ARROW_WIDTH;
  456.   } else {
  457.     subMenuExtension = 0;
  458.   }
  459.   if (item->menu->highlighted == item) {
  460.     gc = whiteGC;
  461.   } else {
  462.     gc = grayGC;
  463.   }
  464.   y = MENU_GAP + fontHeight * num - menuFont->descent;
  465.   XFillRectangle(__glutDisplay, win, gc,
  466.     MENU_GAP, y - fontHeight + menuFont->descent,
  467.     item->menu->pixwidth + subMenuExtension, fontHeight);
  468.   XDrawString(__glutDisplay, win, blackGC,
  469.     MENU_GAP, y, item->label, item->len);
  470.   if (item->isTrigger) {
  471.     paintSubMenuArrow(win,
  472.       item->menu->pixwidth + MENU_ARROW_GAP + 1, y);
  473.   }
  474. }
  475.  
  476. void
  477. __glutPaintMenu(GLUTmenu * menu)
  478. {
  479.   GLUTmenuItem *item;
  480.   int i = menu->num;
  481.   int y = MENU_GAP + fontHeight * i - menuFont->descent;
  482.  
  483.   item = menu->list;
  484.   while (item) {
  485.     if (item->menu->highlighted == item) {
  486.       paintMenuItem(item, i);
  487.     } else {
  488.       /* quick render of the menu item; assume background
  489.          already cleared to gray */
  490.       XDrawString(__glutDisplay, menu->win, blackGC,
  491.         2, y, item->label, item->len);
  492.       if (item->isTrigger) {
  493.         paintSubMenuArrow(menu->win,
  494.           menu->pixwidth + MENU_ARROW_GAP + 1, y);
  495.       }
  496.     }
  497.     i--;
  498.     y -= fontHeight;
  499.     item = item->next;
  500.   }
  501. }
  502.  
  503. GLUTmenuItem *
  504. __glutGetMenuItem(GLUTmenu * menu, Window win, int *which)
  505. {
  506.   GLUTmenuItem *item;
  507.   int i;
  508.  
  509.   i = menu->num;
  510.   item = menu->list;
  511.   while (item) {
  512.     if (item->win == win) {
  513.       *which = i;
  514.       return item;
  515.     }
  516.     if (item->isTrigger) {
  517.       GLUTmenuItem *subitem;
  518.  
  519.       subitem = __glutGetMenuItem(menuList[item->value],
  520.         win, which);
  521.       if (subitem) {
  522.         return subitem;
  523.       }
  524.     }
  525.     i--;
  526.     item = item->next;
  527.   }
  528.   return NULL;
  529. }
  530.  
  531. static int
  532. getMenuItemIndex(GLUTmenuItem * item)
  533. {
  534.   int count = 0;
  535.  
  536.   while (item) {
  537.     count++;
  538.     item = item->next;
  539.   }
  540.   return count;
  541. }
  542.  
  543. GLUTmenu *
  544. __glutGetMenu(Window win)
  545. {
  546.   GLUTmenu *menu;
  547.  
  548.   menu = __glutMappedMenu;
  549.   while (menu) {
  550.     if (win == menu->win) {
  551.       return menu;
  552.     }
  553.     menu = menu->cascade;
  554.   }
  555.   return NULL;
  556. }
  557.  
  558. GLUTmenu *
  559. __glutGetMenuByNum(int menunum)
  560. {
  561.   if (menunum < 1 || menunum > menuListSize) {
  562.     return NULL;
  563.   }
  564.   return menuList[menunum - 1];
  565. }
  566.  
  567. static int
  568. getUnusedMenuSlot(void)
  569. {
  570.   int i;
  571.  
  572.   /* Look for allocated, unused slot. */
  573.   for (i = 0; i < menuListSize; i++) {
  574.     if (!menuList[i]) {
  575.       return i;
  576.     }
  577.   }
  578.   /* Allocate a new slot. */
  579.   menuListSize++;
  580.   if (menuList) {
  581.     menuList = (GLUTmenu **)
  582.       realloc(menuList, menuListSize * sizeof(GLUTmenu *));
  583.   } else {
  584.     /* XXX Some realloc's do not correctly perform a malloc
  585.        when asked to perform a realloc on a NULL pointer,
  586.        though the ANSI C library spec requires this. */
  587.     menuList = (GLUTmenu **) malloc(sizeof(GLUTmenu *));
  588.   }
  589.   if (!menuList)
  590.     __glutFatalError("out of memory.");
  591.   menuList[menuListSize - 1] = NULL;
  592.   return menuListSize - 1;
  593. }
  594.  
  595. static void
  596. menuModificationError(void)
  597. {
  598.   /* XXX Remove the warning after GLUT 3.0. */
  599.   __glutWarning("The following is a new check for GLUT 3.0; update your code.");
  600.   __glutFatalError("menu manipulation not allowed while menus in use");
  601. }
  602.  
  603. int
  604. glutCreateMenu(GLUTselectCB selectFunc)
  605. {
  606.   XSetWindowAttributes wa;
  607.   GLUTmenu *menu;
  608.   int menuid;
  609.  
  610.   if (__glutMappedMenu)
  611.     menuModificationError();
  612.   if (!__glutDisplay)
  613.     __glutOpenXConnection(NULL);
  614.   menuid = getUnusedMenuSlot();
  615.   menu = (GLUTmenu *) malloc(sizeof(GLUTmenu));
  616.   if (!menu)
  617.     __glutFatalError("out of memory.");
  618.   menu->id = menuid;
  619.   menu->num = 0;
  620.   menu->submenus = 0;
  621.   menu->managed = False;
  622.   menu->pixwidth = 0;
  623.   menu->select = selectFunc;
  624.   menu->list = NULL;
  625.   menu->cascade = NULL;
  626.   menu->highlighted = NULL;
  627.   menu->anchor = NULL;
  628.   menuSetup();
  629.   wa.override_redirect = True;
  630.   wa.background_pixel = menuGray;
  631.   wa.border_pixel = menuBlack;
  632.   wa.colormap = menuColormap;
  633.   wa.event_mask = StructureNotifyMask | ExposureMask |
  634.     ButtonPressMask | ButtonReleaseMask |
  635.     EnterWindowMask | LeaveWindowMask;
  636.   /* Save unders really only enabled if useSaveUnders is set to
  637.      CWSaveUnder, ie. using Mesa 3D.  See earlier comments. */
  638.   wa.save_under = True;
  639.   menu->win = XCreateWindow(__glutDisplay, __glutRoot,
  640.   /* real position determined when mapped */
  641.     0, 0,
  642.   /* real size will be determined when menu is manged */
  643.     1, 1,
  644.     MENU_BORDER, menuDepth, InputOutput, menuVisual,
  645.     CWOverrideRedirect | CWBackPixel | CWBorderPixel |
  646.     CWEventMask | CWColormap | useSaveUnders,
  647.     &wa);
  648.   menuGraphicsContextSetup(menu->win);
  649.   menuList[menuid] = menu;
  650.   __glutSetMenu(menu);
  651.   return menuid + 1;
  652. }
  653.  
  654. /* CENTRY */
  655. void
  656. glutDestroyMenu(int menunum)
  657. {
  658.   GLUTmenu *menu = __glutGetMenuByNum(menunum);
  659.   GLUTmenuItem *item, *next;
  660.  
  661.   if (__glutMappedMenu)
  662.     menuModificationError();
  663.   assert(menu->id == menunum - 1);
  664.   XDestroySubwindows(__glutDisplay, menu->win);
  665.   XDestroyWindow(__glutDisplay, menu->win);
  666.   menuList[menunum - 1] = NULL;
  667.   /* free all menu entries */
  668.   item = menu->list;
  669.   while (item) {
  670.     assert(item->menu == menu);
  671.     next = item->next;
  672.     free(item->label);
  673.     free(item);
  674.     item = next;
  675.   }
  676.   if (__glutCurrentMenu == menu) {
  677.     __glutCurrentMenu = NULL;
  678.   }
  679.   free(menu);
  680. }
  681.  
  682. int
  683. glutGetMenu(void)
  684. {
  685.   if (__glutCurrentMenu) {
  686.     return __glutCurrentMenu->id + 1;
  687.   } else {
  688.     return 0;
  689.   }
  690. }
  691.  
  692. void
  693. glutSetMenu(int menuid)
  694. {
  695.   GLUTmenu *menu;
  696.  
  697.   if (menuid < 1 || menuid > menuListSize) {
  698.     __glutWarning("glutSetMenu attempted on bogus menu.");
  699.     return;
  700.   }
  701.   menu = menuList[menuid - 1];
  702.   if (!menu) {
  703.     __glutWarning("glutSetMenu attempted on bogus menu.");
  704.     return;
  705.   }
  706.   __glutSetMenu(menu);
  707. }
  708. /* ENDCENTRY */
  709.  
  710. static void
  711. setMenuItem(GLUTmenuItem * item, char *label,
  712.   int value, Bool isTrigger)
  713. {
  714.   GLUTmenu *menu;
  715.  
  716.   menu = item->menu;
  717.   item->label = strdup(label);
  718.   if (!item->label)
  719.     __glutFatalError("out of memory.");
  720.   item->isTrigger = isTrigger;
  721.   item->len = (int) strlen(label);
  722.   item->value = value;
  723.   item->pixwidth = XTextWidth(menuFont, label, item->len) + 4;
  724.   if (item->pixwidth > menu->pixwidth) {
  725.     menu->pixwidth = item->pixwidth;
  726.   }
  727.   menu->managed = False;
  728. }
  729.  
  730. /* CENTRY */
  731. void
  732. glutAddMenuEntry(char *label, int value)
  733. {
  734.   XSetWindowAttributes wa;
  735.   GLUTmenuItem *entry;
  736.  
  737.   if (__glutMappedMenu)
  738.     menuModificationError();
  739.   entry = (GLUTmenuItem *) malloc(sizeof(GLUTmenuItem));
  740.   if (!entry)
  741.     __glutFatalError("out of memory.");
  742.   entry->menu = __glutCurrentMenu;
  743.   setMenuItem(entry, label, value, False);
  744.   wa.event_mask = EnterWindowMask | LeaveWindowMask;
  745.   entry->win = XCreateWindow(__glutDisplay,
  746.     __glutCurrentMenu->win, MENU_GAP,
  747.     __glutCurrentMenu->num * fontHeight + MENU_GAP,  /* x & y */
  748.     entry->pixwidth, fontHeight,  /* width & height */
  749.     0, CopyFromParent, InputOnly, CopyFromParent,
  750.     CWEventMask, &wa);
  751.   XMapWindow(__glutDisplay, entry->win);
  752.   __glutCurrentMenu->num++;
  753.   entry->next = __glutCurrentMenu->list;
  754.   __glutCurrentMenu->list = entry;
  755. }
  756.  
  757. void
  758. glutAddSubMenu(char *label, int menu)
  759. {
  760.   XSetWindowAttributes wa;
  761.   GLUTmenuItem *submenu;
  762.  
  763.   if (__glutMappedMenu)
  764.     menuModificationError();
  765.   submenu = (GLUTmenuItem *) malloc(sizeof(GLUTmenuItem));
  766.   if (!submenu)
  767.     __glutFatalError("out of memory.");
  768.   __glutCurrentMenu->submenus++;
  769.   submenu->menu = __glutCurrentMenu;
  770.   setMenuItem(submenu, label, /* base 0 */ menu - 1, True);
  771.   wa.event_mask = EnterWindowMask | LeaveWindowMask;
  772.   submenu->win = XCreateWindow(__glutDisplay,
  773.     __glutCurrentMenu->win, MENU_GAP,
  774.     __glutCurrentMenu->num * fontHeight + MENU_GAP,  /* x & y */
  775.     submenu->pixwidth, fontHeight,  /* width & height */
  776.     0, CopyFromParent, InputOnly, CopyFromParent,
  777.     CWEventMask, &wa);
  778.   XMapWindow(__glutDisplay, submenu->win);
  779.   __glutCurrentMenu->num++;
  780.   submenu->next = __glutCurrentMenu->list;
  781.   __glutCurrentMenu->list = submenu;
  782. }
  783.  
  784. void
  785. glutChangeToMenuEntry(int num, char *label, int value)
  786. {
  787.   GLUTmenuItem *item;
  788.   int i;
  789.  
  790.   if (__glutMappedMenu)
  791.     menuModificationError();
  792.   i = __glutCurrentMenu->num;
  793.   item = __glutCurrentMenu->list;
  794.   while (item) {
  795.     if (i == num) {
  796.       if (item->isTrigger) {
  797.         /* If changing a submenu trigger to a menu entry, we
  798.            need to account for submenus.  */
  799.         item->menu->submenus--;
  800.       }
  801.       free(item->label);
  802.       setMenuItem(item, label, value, False);
  803.       return;
  804.     }
  805.     i--;
  806.     item = item->next;
  807.   }
  808.   __glutWarning("Current menu has no %d item.", num);
  809. }
  810.  
  811. void
  812. glutChangeToSubMenu(int num, char *label, int menu)
  813. {
  814.   GLUTmenuItem *item;
  815.   int i;
  816.  
  817.   if (__glutMappedMenu)
  818.     menuModificationError();
  819.   i = __glutCurrentMenu->num;
  820.   item = __glutCurrentMenu->list;
  821.   while (item) {
  822.     if (i == num) {
  823.       if (!item->isTrigger) {
  824.         /* If changing a menu entry to as submenu trigger, we
  825.            need to account for submenus.  */
  826.         item->menu->submenus++;
  827.       }
  828.       free(item->label);
  829.       setMenuItem(item, label, /* base 0 */ menu - 1, True);
  830.       return;
  831.     }
  832.     i--;
  833.     item = item->next;
  834.   }
  835.   __glutWarning("Current menu has no %d item.", num);
  836. }
  837.  
  838. void
  839. glutRemoveMenuItem(int num)
  840. {
  841.   GLUTmenuItem *item, **prev, *remaining;
  842.   int pixwidth, i;
  843.  
  844.   if (__glutMappedMenu)
  845.     menuModificationError();
  846.   i = __glutCurrentMenu->num;
  847.   prev = &__glutCurrentMenu->list;
  848.   item = __glutCurrentMenu->list;
  849.   /* If menu item is removed, the menu's pixwidth may need to
  850.      be recomputed. */
  851.   pixwidth = 0;
  852.   while (item) {
  853.     if (i == num) {
  854.       /* If this menu item's pixwidth is as wide as the menu's
  855.          pixwidth, removing this menu item will necessitate
  856.          shrinking the menu's pixwidth. */
  857.       if (item->pixwidth >= __glutCurrentMenu->pixwidth) {
  858.         /* Continue recalculating menu pixwidth, first skipping
  859.            the removed item. */
  860.         remaining = item->next;
  861.         while (remaining) {
  862.           if (remaining->pixwidth > pixwidth) {
  863.             pixwidth = remaining->pixwidth;
  864.           }
  865.           remaining = remaining->next;
  866.         }
  867.       }
  868.       __glutCurrentMenu->num--;
  869.       __glutCurrentMenu->managed = False;
  870.       __glutCurrentMenu->pixwidth = pixwidth;
  871.  
  872.       /* Patch up menu's item list. */
  873.       *prev = item->next;
  874.  
  875.       free(item->label);
  876.       free(item);
  877.       return;
  878.     }
  879.     if (item->pixwidth > pixwidth) {
  880.       pixwidth = item->pixwidth;
  881.     }
  882.     i--;
  883.     prev = &item->next;
  884.     item = item->next;
  885.   }
  886.   __glutWarning("Current menu has no %d item.", num);
  887. }
  888.  
  889. void
  890. glutAttachMenu(int button)
  891. {
  892.   if (__glutMappedMenu)
  893.     menuModificationError();
  894.   if (__glutCurrentWindow->menu[button] < 1) {
  895.     __glutCurrentWindow->buttonUses++;
  896.   }
  897.   __glutChangeWindowEventMask(
  898.     ButtonPressMask | ButtonReleaseMask, True);
  899.   __glutCurrentWindow->menu[button] = __glutCurrentMenu->id + 1;
  900. }
  901.  
  902. void
  903. glutDetachMenu(int button)
  904. {
  905.   if (__glutMappedMenu)
  906.     menuModificationError();
  907.   if (__glutCurrentWindow->menu[button] > 0) {
  908.     __glutCurrentWindow->buttonUses--;
  909.     __glutChangeWindowEventMask(ButtonPressMask | ButtonReleaseMask,
  910.       __glutCurrentWindow->buttonUses > 0);
  911.     __glutCurrentWindow->menu[button] = 0;
  912.   }
  913. }
  914. /* ENDCENTRY */
  915.  
  916. void
  917. __glutMenuItemEnterOrLeave(GLUTmenuItem * item,
  918.   int num, int type)
  919. {
  920.   int alreadyUp = 0;
  921.  
  922.   if (type == EnterNotify) {
  923.     GLUTmenuItem *prevItem = item->menu->highlighted;
  924.  
  925.     if (prevItem && prevItem != item) {
  926.       /* If there's an already higlighted item in this menu
  927.          that is different from this one (we could be
  928.          re-entering an item with an already cascaded
  929.          submenu!), unhighlight the previous item. */
  930.       item->menu->highlighted = NULL;
  931.       paintMenuItem(prevItem, getMenuItemIndex(prevItem));
  932.     }
  933.     item->menu->highlighted = item;
  934.     __glutItemSelected = item;
  935.     if (item->menu->cascade) {
  936.       if (!item->isTrigger) {
  937.         /* Entered a menu item that is not a submenu trigger,
  938.            so pop down the current submenu cascade of this
  939.            menu.  */
  940.         unmapMenu(item->menu->cascade);
  941.         item->menu->cascade = NULL;
  942.       } else {
  943.         GLUTmenu *submenu = menuList[item->value];
  944.  
  945.         if (submenu->anchor == item) {
  946.           /* We entered the submenu trigger for the submenu
  947.              that is already up, so don't take down the
  948.              submenu.  */
  949.           alreadyUp = 1;
  950.         } else {
  951.           /* Submenu already popped up for some other submenu
  952.              item of this menu; need to pop down that other
  953.              submenu cascade.  */
  954.           unmapMenu(item->menu->cascade);
  955.           item->menu->cascade = NULL;
  956.         }
  957.       }
  958.     }
  959.     if (!alreadyUp) {
  960.       /* Make sure the menu item gets painted with
  961.          highlighting. */
  962.       paintMenuItem(item, num);
  963.     } else {
  964.       /* If already up, should already be highlighted.  */
  965.     }
  966.   } else {
  967.     /* LeaveNotify: Handle leaving a menu item...  */
  968.     if (item->menu->cascade &&
  969.       item->menu->cascade->anchor == item) {
  970.       /* If there is a submenu casacaded from this item, do not
  971.          change the highlighting on this item upon leaving. */
  972.     } else {
  973.       /* Unhighlight this menu item.  */
  974.       item->menu->highlighted = NULL;
  975.       paintMenuItem(item, num);
  976.     }
  977.     __glutItemSelected = NULL;
  978.   }
  979.   if (item->isTrigger) {
  980.     if (type == EnterNotify && !alreadyUp) {
  981.       GLUTmenu *submenu = menuList[item->value];
  982.  
  983.       mapMenu(submenu,
  984.         item->menu->x + item->menu->pixwidth +
  985.         MENU_ARROW_GAP + MENU_ARROW_WIDTH +
  986.         MENU_GAP + MENU_BORDER,
  987.         item->menu->y + fontHeight * (num - 1) + MENU_GAP);
  988.       item->menu->cascade = submenu;
  989.       submenu->anchor = item;
  990.     }
  991.   }
  992. }
  993.